﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BruteForce.Utilities
{
    public static class Draws
    {
        private static readonly SpriteBatch SBatch = BFGame.SpriteBatch;

        /// <summary>
        /// return the size (width/height) of the normal font on M metrics
        /// </summary>
        public static Vector2 NormalFontSize
        {
            get { return BFGame.NormalFont.MeasureString("M"); }
        }

        /// <summary>
        /// draw string using the normal font colored white on the given positionS
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="text"></param>
        public static void DrawString(int x, int y, string text)
        {
            if (text != null)
                SBatch.DrawString(BFGame.NormalFont, text, new Vector2(x, y), Color.White);
        }

        public static void DrawString(Location loc, string text)
        {
            if (text != null)
                DrawString(loc.OnScreenX, loc.OnScreenY, text);
        }

        /// <summary>
        /// draw string using the normal font colored white on the given positionS
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="text"></param>
        public static void DrawString(int x, int y, string text, Color color)
        {
            if (text != null)
            {
                SBatch.DrawString(BFGame.NormalFont, text, new Vector2(x, y), color);
            }
        }


        /// <summary>
        /// return the size (width/height) of the normal font on the parameter received metrics Calculate
        /// </summary>
        public static Vector2 CalculateWordSizeNormalFont(string word)
        {
            return BFGame.NormalFont.MeasureString(word);
        }

        public static void DrawBorder(Rectangle rect, Color color, int width)
        {
            //|
            DrawLine(rect.X + width, rect.Y, rect.X + width, rect.Y + rect.Height, color, width);
            //-
            DrawLine(rect.X, rect.Y, rect.X + rect.Width, rect.Y, color, width);
            //_
            DrawLine(rect.X + rect.Width, rect.Y + rect.Height, rect.X, rect.Y + rect.Height, color, width);
            //..|
            DrawLine(rect.X + rect.Width, rect.Y, rect.X + rect.Width, rect.Y + rect.Height, color, width);
        }

        public static void DrawBorder(Location rect, Color color, int width)
        {
            DrawBorder(rect.Position, color, width);
        }

        public static void DrawLine(Vector4 line, Color color, int width)
        {
            DrawLine((int) line.X, (int) line.Y, (int) line.Z, (int) line.W, color, width);
        }

        public static void DrawLine(int x1, int y1, int x2, int y2, Color color, int width)
        {
            var p1 = new Vector2(x1, y1);
            var p2 = new Vector2(x2, y2);
            var angle = (float) Math.Atan2(p2.Y - p1.Y, p2.X - p1.X);
            float dist = Vector2.Distance(p1, p2);
            BFGame.SpriteBatch.Draw(BFGame.Blank, p1, null, color, angle, Vector2.Zero, new Vector2(dist, width),
                                    SpriteEffects.None, 0);
        }

        public static void DrawBlank(Rectangle where, Color color)
        {
            BFGame.SpriteBatch.Draw(BFGame.Blank, where, color);
        }

        public static void DrawBlank(Location where, Color color)
        {
            BFGame.SpriteBatch.Draw(BFGame.Blank, where.Position, color);
        }
    }
}